<?php
/**
 * 核心文件
 * @author Yangdong Zhang <zhangyangdong@gouuse.cn>
 * @time 2018/10/17 9:48
 */
namespace App;

class Application
{
    protected $mod;
    protected $act;

    protected $config;

    /**
     * Application constructor.
     * 入口
     * @param array $database
     * @param array $config
     */
    public function __construct($database=[], $config=[])
    {
        //配置数据库
        \think\Db::setConfig($database);

        $this->config = $config;

        $mod = 'Index';
        $act = 'index';
        $request = $_REQUEST;

        $path = [];
        //pathInfo
        if ($this->config['path_info']) {
            //没得index.php
            if (isset($request['_url'])) {
                $path = explode('/', ltrim($request['_url'], '/'));
            }
            //有index.php
            if (isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO']) {
                $path = explode('/', ltrim($_SERVER['PATH_INFO'], '/'));
            }
        }else {
            //传统模式
            if (isset($request['mod'])) $path['0'] = $request['mod'];
            if (isset($request['act'])) $path['1'] = $request['act'];
        }

        $this->mod = (string)ucfirst($path['0'] ?? $mod);
        $this->act = (string)lcfirst($path['1'] ?? $act);

    }

    /**
     * 运行
     */
    public function runMvc()
    {
        if (isset($this->config['namespace'])) {
            $model = $this->config['namespace'].$this->mod;
        }else {
            $model = '\\App\\Controller\\'.$this->mod;
        }

        $app  = new $model();
        call_user_func_array([$app, $this->act], ['test' => 111]);
    }
}